Partial Class Admin_Add_General_LookUp
  Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 1, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.EditType)
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing
    End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Session("LookUp") <> Nothing Then
      ViewState("LookUp") = Session("LookUp")
      Session.Remove("LookUp")
      Dim zClass As New LookUp.General_Lookup
      zClass.LookUpSelected(ViewState("LookUp").ToString, Field_lbl, Count_lbl, Add_btn, DataGrid)
      zClass.LookUpDecode(ViewState("LookUp").ToString)
      zClass.DataGridFill(DataGrid, Results_lbl)

    ElseIf Session("LookUp") = Nothing And ViewState("LookUp") <> Nothing Then
      Dim zClass As New LookUp.General_Lookup
      zClass.LookUpSelected(ViewState("LookUp").ToString, Field_lbl, Count_lbl, Add_btn, DataGrid)
      zClass.LookUpDecode(ViewState("LookUp").ToString)
      zClass.DataGridFill(DataGrid, Results_lbl)

    Else
      'put error handling here
      MsgBox("error")

    End If

        SelectMenu()

        rfv1.ErrorMessage = Field_lbl.Text & " Required"
        SecurePage()
  End Sub

  Protected Sub DataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.DeleteCommand
    Dim zClass As New LookUp.General_Lookup
    zClass.LookUpDecode(ViewState("LookUp").ToString)

    zClass.ExecQuery("DELETE FROM lookup_mstr WHERE lookup_cd='" & zClass.LookUpCode & _
      "' AND lookup_seq='" & e.Item.Cells(0).Text & "'")

    DataGrid.CurrentPageIndex = 0
    zClass.DataGridFill(DataGrid, Results_lbl)

  End Sub

  Protected Sub DataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.EditCommand
    ViewState("seq") = e.Item.Cells(0).Text
    ViewState("mode") = "edit"
    Field_txt.Text = e.Item.Cells(1).Text
    OnEditMode()
  End Sub

  Private Sub OnEditMode()
    DataGrid.Enabled = False : Add_btn.Enabled = False : Edit_Panel.Visible = True : Field_txt.Focus()
  End Sub

  Private Sub NotOnEditMode()
    DataGrid.Enabled = True : Add_btn.Enabled = True : Edit_Panel.Visible = False
  End Sub

  Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
    NotOnEditMode()
  End Sub

  Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO
        zClass.LookUpDecode(ViewState("LookUp").ToString)

        If ViewState("mode") = "edit" Then
            zClass.ExecQuery("UPDATE lookup_mstr SET lookup_desc=" & UCase(dm.Q(Field_txt.Text)) & _
              ", lookup_last_upd_by='" & UCase(Session("UserName")) & "', lookup_last_upd_dt='" & _
              Now & _
              "' WHERE lookup_cd='" & zClass.LookUpCode & "' AND lookup_seq='" & ViewState("seq") & "'")

        ElseIf ViewState("mode") = "add" Then
            zClass.ExecQuery("INSERT INTO lookup_mstr(lookup_seq,lookup_cd,lookup_desc," & _
              "lookup_crea_by,lookup_crea_dt) VALUES ('" & _
              zClass.MaxCode() & "','" & UCase(zClass.LookUpCode) & "'," & UCase(dm.Q(Field_txt.Text)) & _
              ",'" & Session("UserName") & "','" & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & "')")
        End If

    ViewState.Remove("seq") : ViewState.Remove("mode")
    DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl)
        NotOnEditMode()
  End Sub

  Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
    ViewState("mode") = "add" : Field_txt.Text = "" : OnEditMode()
  End Sub

  Private Sub SelectMenu()
        Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Lookup_Panel"), Panel)
        Edit_Panel.Visible = True

        Dim Edit_Image As New Image
        Dim Edit_Button As New LinkButton

        Select Case ViewState("LookUp").ToString
            Case "Advisor Levels"
                Edit_Image = CType(Me.Master.FindControl("L1"), Image)
                Edit_Button = CType(Me.Master.FindControl("AdvisorLevels_btn"), LinkButton)

            Case "Beneficiary Ranking"
                Edit_Image = CType(Me.Master.FindControl("L2"), Image)
                Edit_Button = CType(Me.Master.FindControl("BeneficiaryRanking_btn"), LinkButton)

            Case "Client Types"
                Edit_Image = CType(Me.Master.FindControl("L4"), Image)
                Edit_Button = CType(Me.Master.FindControl("ClientTypes_btn"), LinkButton)

            Case "Correspondence Status"
                Edit_Image = CType(Me.Master.FindControl("L5"), Image)
                Edit_Button = CType(Me.Master.FindControl("CorrespondenceStatus_btn"), LinkButton)

            Case "Commission Status"
                Edit_Image = CType(Me.Master.FindControl("C2"), Image)
                Edit_Button = CType(Me.Master.FindControl("CommissionStatus_btn"), LinkButton)

            Case "Commission Type"
                Edit_Image = CType(Me.Master.FindControl("C3"), Image)
                Edit_Button = CType(Me.Master.FindControl("CommissionType_btn"), LinkButton)

            Case "Departments"
                Edit_Image = CType(Me.Master.FindControl("L7"), Image)
                Edit_Button = CType(Me.Master.FindControl("Departments_btn"), LinkButton)

            Case "Forms of Payment"
                Edit_Image = CType(Me.Master.FindControl("L8"), Image)
                Edit_Button = CType(Me.Master.FindControl("FormsofPayment_btn"), LinkButton)

            Case "Frequency of Payment"
                Edit_Image = CType(Me.Master.FindControl("L9"), Image)
                Edit_Button = CType(Me.Master.FindControl("FrequencyofPayment_btn"), LinkButton)

            Case "Fund Currency"
                Edit_Image = CType(Me.Master.FindControl("L10"), Image)
                Edit_Button = CType(Me.Master.FindControl("FundCurrency_btn"), LinkButton)

            Case "ID Types"
                Edit_Image = CType(Me.Master.FindControl("L12"), Image)
                Edit_Button = CType(Me.Master.FindControl("IDTypes_btn"), LinkButton)

            Case "Issue Status"
                Edit_Image = CType(Me.Master.FindControl("L13"), Image)
                Edit_Button = CType(Me.Master.FindControl("IssueStatus_btn"), LinkButton)

            Case "Marital Status"
                Edit_Image = CType(Me.Master.FindControl("L23"), Image)
                Edit_Button = CType(Me.Master.FindControl("MaritalStatus_btn"), LinkButton)

            Case "Medical Rating"
                Edit_Image = CType(Me.Master.FindControl("L14"), Image)
                Edit_Button = CType(Me.Master.FindControl("MedicalRating_btn"), LinkButton)

            Case "Nationalities"
                Edit_Image = CType(Me.Master.FindControl("L15"), Image)
                Edit_Button = CType(Me.Master.FindControl("Nationalities_btn"), LinkButton)

            Case "Payment Status"
                Edit_Image = CType(Me.Master.FindControl("L16"), Image)
                Edit_Button = CType(Me.Master.FindControl("PaymentStatus_btn"), LinkButton)

            Case "Plan Types"
                Edit_Image = CType(Me.Master.FindControl("L17"), Image)
                Edit_Button = CType(Me.Master.FindControl("PlanTypes_btn"), LinkButton)

            Case "Policy Status"
                Edit_Image = CType(Me.Master.FindControl("L18"), Image)
                Edit_Button = CType(Me.Master.FindControl("PolicyStatus_btn"), LinkButton)

            Case "Relationship"
                Edit_Image = CType(Me.Master.FindControl("L24"), Image)
                Edit_Button = CType(Me.Master.FindControl("Relationship_btn"), LinkButton)

            Case "Smoking Status"
                Edit_Image = CType(Me.Master.FindControl("L21"), Image)
                Edit_Button = CType(Me.Master.FindControl("SmokingStatus_btn"), LinkButton)

            Case "Source of Funds"
                Edit_Image = CType(Me.Master.FindControl("L22"), Image)
                Edit_Button = CType(Me.Master.FindControl("SourceofFunds_btn"), LinkButton)

        End Select

        Edit_Image.ImageUrl = "~/images/redT.PNG"
        Edit_Button.ForeColor = Drawing.Color.Black

  End Sub

  Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        Dim zClass As New LookUp.General_Lookup

        DataGrid.CurrentPageIndex = e.NewPageIndex
        zClass.LookUpSelected(ViewState("LookUp").ToString, Field_lbl, Count_lbl, Add_btn, DataGrid)
        zClass.LookUpDecode(ViewState("LookUp").ToString)
        zClass.DataGridFill(DataGrid, Results_lbl)

        '    DataGrid.CurrentPageIndex = e.NewPageIndex
        'Dim zClass As New LookUp.General_Lookup
        'zClass.LookUpDecode(ViewState("LookUp").ToString)
        'zClass.DataGridFill(DataGrid, Results_lbl)
    End Sub

End Class